package com.suikajy.damaiproject.ui;

import android.os.Bundle;
import android.os.Handler;
import android.os.PersistableBundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.util.SparseArray;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.suikajy.damaiproject.App;
import com.suikajy.damaiproject.R;
import com.suikajy.damaiproject.ui.main_class.ClassFragment;
import com.suikajy.damaiproject.ui.main_damai.DamaiFragment;
import com.suikajy.damaiproject.ui.main_home.HomeFragment;
import com.suikajy.damaiproject.ui.main_set.SetFragment;

import butterknife.Bind;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {

    @Bind(R.id.rb_home)
    RadioButton mRbHome;
    @Bind(R.id.rb_class)
    RadioButton mRbClass;
    @Bind(R.id.rb_damai)
    RadioButton mRbDamai;
    @Bind(R.id.rb_set)
    RadioButton mRbSet;
    @Bind(R.id.rg_main)
    RadioGroup mRgMain;
    @Bind(R.id.activity_main)
    RelativeLayout mActivityMain;
    private SparseArray<Fragment> mFragments = new SparseArray<>();
    private Fragment mCurrentFragment;
    private FragmentManager mFragmentManager;
    private Handler mHandler = new Handler();
    private boolean isFinish = false;
    private Toast mBackToast = Toast.makeText(App.getInstance(), "再次点击返回键退出", Toast.LENGTH_SHORT);
    private static MainActivity instance;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        instance = this;
        initFragment();
        initView();
    }

    private void initFragment() {
        Fragment homeFragment = new HomeFragment();
        Fragment classFragment = new ClassFragment();
        Fragment damaiFragment = new DamaiFragment();
        Fragment setFragment = new SetFragment();
        mFragments.put(0, homeFragment);
        mFragments.put(1, classFragment);
        mFragments.put(2, damaiFragment);
        mFragments.put(3, setFragment);
    }

    private void initView() {
        mRgMain.setOnCheckedChangeListener(this);
        mFragmentManager = getSupportFragmentManager();
        mFragmentManager.beginTransaction().add(R.id.fl_main, mFragments.get(0)).commit();
        mCurrentFragment = mFragments.get(0);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.rb_home:
                switchFragment(mFragments.get(0));
                break;
            case R.id.rb_class:
                switchFragment(mFragments.get(1));
                break;
            case R.id.rb_damai:
                switchFragment(mFragments.get(2));
                break;
            case R.id.rb_set:
                switchFragment(mFragments.get(3));
                break;
        }
    }

    private void switchFragment(Fragment toFragment) {
        if (toFragment.isAdded()) {
            mFragmentManager.beginTransaction()
                    .hide(mCurrentFragment)
                    .show(toFragment)
                    .commit();
        } else {
            mFragmentManager.beginTransaction()
                    .hide(mCurrentFragment)
                    .add(R.id.fl_main, toFragment)
                    .commit();
        }
        mCurrentFragment = toFragment;
    }

    public static MainActivity getInstance() {
        return instance;
    }

    @Override
    public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
        instance = null;
        super.onSaveInstanceState(outState, outPersistentState);
    }

    @Override
    public void onBackPressed() {
        if (isFinish) {
            mBackToast.cancel();
            instance = null;
            finish();
        } else {
            mBackToast.show();
        }
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                isFinish = false;
            }
        }, 2000);
        isFinish = true;
    }

    @Override
    protected void onDestroy() {
        instance = null;
        super.onDestroy();
    }
}
